
//
//  GoodsDetailHeaderView.m
//  FD
//
//  Created by xiaofang wang on 2018/12/7.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
// //250 + 50 + 20 + x

#import "GoodsDetailHeaderView.h"
//model
#import "HomeGoodsDetailHeaderModel.h"

@interface GoodsDetailHeaderView()<UIWebViewDelegate>
/**1.图片*/
@property (nonatomic, strong) UIImageView *imgIV;
/**2.名字 暂定写死*/
@property (nonatomic, strong) UILabel *nameLb;
/**3.现价价格LB*/
@property (nonatomic, strong) UILabel *currentPriceLb;
/**3-2原价lb*/
@property(nonatomic, strong) UILabel *originPriceLb;

/**4.webView网页 需要高度自适应*/
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation GoodsDetailHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    
    return self;
}

- (void)setupView {
    
    [self addSubview:self.imgIV];
    [self addSubview:self.nameLb];
    [self addSubview:self.currentPriceLb];
    [self addSubview:self.originPriceLb]; //2019-2-16
    
    [self addSubview:self.webView];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    //FDScaleWidth(250) + 50 + 20 + x
    //左右没有间距
    [self.imgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);//.offset(15);
        make.top.equalTo(self.mas_top);
        make.right.equalTo(self.mas_right);//.offset(-15);
        make.height.mas_equalTo(FDScaleWidth(250)); //(200);
        //
    }];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.top.equalTo(self.imgIV.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    [self.currentPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb.mas_left);
        make.width.mas_greaterThanOrEqualTo(60); //equalTo(self.nameLb.mas_right);
        make.top.equalTo(self.nameLb.mas_bottom);
        make.height.mas_equalTo(20);
    }];
    [self.originPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.currentPriceLb.mas_right).offset(5);
        make.top.equalTo(self.currentPriceLb.mas_top);
        make.width.mas_greaterThanOrEqualTo(50);//内容自适应
        make.height.mas_equalTo(20);
    }];
    
    
    
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.currentPriceLb.mas_bottom);
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.bottom.equalTo(self.mas_bottom); //.offset(5);
    }];
    
    
    
//    [self.imgIV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.equalTo(self);
//        make.height.mas_equalTo(200);
//    }];
//
//    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.mas_left).offset(15);
//        make.right.equalTo(self.mas_right).offset(-15);
//        make.top.equalTo(self.imgIV.mas_bottom);
//        make.height.mas_equalTo(50);
//    }];
//
//    [self.currentPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.mas_left).offset(15);
//        make.right.equalTo(self.mas_right).offset(-15);
//        make.top.equalTo(self.nameLb.mas_bottom);
//        make.height.mas_equalTo(20);
//    }];
//
//    //高度自适应
//    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.currentPriceLb.mas_bottom);
//        make.left.right.equalTo(self);
//        make.bottom.equalTo(self.mas_bottom);
//    }];
}

#pragma mark - getter
- (UIImageView *)imgIV {
    if (!_imgIV) {
        _imgIV = [[UIImageView alloc] init];
    }
    return _imgIV;
}
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [[UILabel alloc] init];
    }
    return _nameLb;
}

- (UILabel *)currentPriceLb {
    if (!_currentPriceLb) {
        _currentPriceLb = [UILabel new];
        _currentPriceLb.textColor = [UIColor redColor];
    }
    return _currentPriceLb;
}
- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [UILabel new];
        _originPriceLb.textColor = [UIColor lightGrayColor];
    }
    return _originPriceLb;
}


- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
        
        //禁止滚动🚫
        _webView.scrollView.scrollEnabled = NO;
    }
    return _webView;
}


#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
    FDLog(@"来了: %@",heightStr);
    
    if (self.goodsDetailHeaderViewHeightBlock) {
        self.goodsDetailHeaderViewHeightBlock([heightStr floatValue] + 15);
        //p标签默认有margin。直接获取是不包含margin值的？
    };
}

#pragma mark - 根据模型进行赋值
- (void)setModel:(HomeGoodsDetailHeaderModel *)model {
    _model = model;
    
    [self.imgIV sd_setImageWithURL:[NSURL URLWithString:model.thumbnail] placeholderImage:nil];
    self.nameLb.text = model.title;


    NSString *fenStr = NSLocalizedString(L_Order_Fen, @"份");
    
    //如果有默认值的化要加上默认值、
    self.currentPriceLb.text = [NSString stringWithFormat:@"€ %.1f/%@",[model.currentPrice floatValue] + [model.defaultSelectPrice floatValue],fenStr];
}

#pragma mark - 接口

- (void)headerViewLoadwebviewWithContentHtmlStr:(NSString *)contnetHtmlStr {

    NSString *htmlString = [FDWebViewHtmlTool fdwebViewReplaceTemplateHtmlMainDivContentWithHtmlStr:contnetHtmlStr];
    
    [self.webView loadHTMLString:htmlString baseURL:nil];
}


- (void)updateGoodsDetailHeaderViewSumCurrentPrice:(CGFloat)sumCP sumOriginPrice:(CGFloat)sumop {
    NSString *fenStr = NSLocalizedString(L_Order_Fen, @"份");
    self.currentPriceLb.text = [NSString stringWithFormat:@"€%.1f/%@",sumCP,fenStr];
    
    
    NSString *centerLineSumopstring = [NSString stringWithFormat:@"€%.1f",sumop];
    //添加中划线
    NSDictionary *attDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:centerLineSumopstring attributes:attDic];
    
    self.originPriceLb.attributedText = attributeStr;
    
    //2019-2-18原价为0或者 不等于现价则不显示
    //((NSInteger)sumCP == (NSInteger)sumop) 这个判断可能有问题
    if (sumop < 0.1 || ((NSInteger)sumCP == (NSInteger)sumop)) {
        self.originPriceLb.hidden = YES;
    }else {
        self.originPriceLb.hidden = NO;
    }
}

@end
